<?php

class Index_AdminAction extends C_Action  
{
	private $user_E = null;
	private $site_E = null;
	private $RootOper = 0;
	public function init()
	{
		$this->user_E = Zend_Registry::get("user_E");
		$this->site_E = Zend_Registry::get("site_E");
		$url = Zend_Registry::get("url");
		$login_url = $this->getUrl("index","auth","token",array(),array("url"=>$url));
		$this->RootOper = Zend_Registry::get('RootOper');
		if($this->RootOper == 0)
		{
			$this->RedirectJump($login_url);
		}
		
	}
	public function doInfo()
	{ 
		
		$title = $this->_request->get("title");
		$descript = $this->_request->get("descript");
		$update = $this->_request->get("update");
		if($update)
		{
			$this->site_E->title= $title;
			$this->site_E->descript = $descript;
			if(empty($title))
			{
				$data['error'] = "房间名字可不能空";
				echo json_encode($data);
				exit;
			}
			if(empty($descript))
			{
				$data['error'] = "告诉你的小站是干什么的吧";
				echo json_encode($data);
				exit;
			}
			$UnitOfWork = E_UnitOfWork::getWork();
			$UnitOfWork->commit();	
			$data['r'] = 0;
		  	echo json_encode($data);
			exit;
		}
	}
	public function doIcon()
	{
		$version = $this->_request->get('version');
		$name = $this->site_E->id;
		if($version)
		{
			$imgpos = $this->_request->get('imgpos');
			$imgpos_arr = explode("_", $imgpos);
			$top = $imgpos_arr[0];
			$left = $imgpos_arr[1];
			$width= $imgpos_arr[2];
			$picid = $this->_request->get('version');
			$pic_E = Pics::getById($picid);
			$picname_pic = $pic_E->pic;
			$first_array = explode(".", $picname_pic);
			$picname = $first_array[0]."_180X240.".$first_array[1];
			$image_real_path = get_image_path();
			$image_absolute_path = $image_real_path.$picname;
			new T_Resizeimage($image_absolute_path , 48,48 ,1,$top,$left,$width);
			$this->site_E->logo = $picname_pic;
			$UnitOfWork = E_UnitOfWork::getWork();
			$UnitOfWork->commit();	
			exit;
		}
	}
	public function doIconupload()
	{
		$name = $this->site_E->id;
		$image_real_path = get_image_path();
		$image_path = get_image_httppath();
		$pic_return = T_Picupload::save_pic('picfile', '/site/icon', '' ,array(100,100));
		if($pic_return['error'] < 0)
		{
				$data['r'] = "1";
				$data['error'] = "图片大小问题";
				echo json_encode($data);
				exit;
		}
		
		$image_url = '/site/icon/'.$pic_return['newname'];
		$row['pic'] = $image_url;
		$row['userid'] = $this->user_E->id;
		$row['sign'] = 1;
		$pic_E = Pics::insertEntity($row);
		$UnitOfWork = E_UnitOfWork::getWork();
		$UnitOfWork->commit();	
		
		
		$image_absolute_path = $image_real_path.'/site/icon/'.$pic_return['newname'];
		new T_Resizeimage($image_absolute_path , 180,240, 0);
	
		
		
		
		$first_array = explode(".", $image_url);
		$image_url = $first_array[0]."_180X240.".$first_array[1];
		
		
		
		
		
		$data['ver'] = $pic_E->id;
		$data['r'] = 0;
		$data['pic'] = $image_url."?".rand();
		echo json_encode($data);
		exit;
		
	}
	public function doDesign()
	{
		$style = $this->site_E->style;
		if(!empty($style))
		{
			$style = unserialize($style);
			$theme_id = $style['theme_id'];
			if(empty($theme_id))
			{
				$theme_id = 1;
			}
			$background = $style['background_pic'];
			if(empty($background))
			{
				$background = "/pics/site/theme/$theme_id/thumbnail.png";
			}
			$sp_nav_transparent  = $style['sp_nav_transparent'];
			$bgfixed = $style['bgfixed'];
		}
		if(empty($theme_id))
		{
			$theme_id = 1;
		}
		$this->_response->theme_id = $theme_id;
		$this->_response->background = $background;
		$this->_response->sp_nav_transparent = $sp_nav_transparent;
		$this->_response->bgfixed = $bgfixed;
	}
	public function doCustom()
	{
		/*
			background_cancel	
			background_color	
			background_pos	  居左 居中 居右  left, right,right
			background_repeat	repeat
			background_ver	   数据库背景图片
			banner_color	
			link_color	
			tab_color	
			tab_link_color	
			theme_id	15
			*/
		
		 $background_cancel = $this->_request->get('background_cancel');
		  $background_pic = $this->_request->get('background_pic');
		 $background_color = $this->_request->get('background_color');
		 $background_pos = $this->_request->get('background_pos');
		 $background_repeat = $this->_request->get('background_repeat');
		 $banner_color = $this->_request->get('banner_color');
		 $link_color = $this->_request->get('link_color');
		 $tab_color = $this->_request->get('tab_color');
		 $tab_link_color = $this->_request->get('tab_link_color');
		 $theme_id = $this->_request->get('theme_id');
		 $picid = $this->_request->get('background_ver');
		 $sp_nav_transparent = $this->_request->get('sp_nav_transparent');
		 $bgfixed = $this->_request->get('bgfixed');
		 $themelogo = $this->_request->get('themelogo');
		 $bgfixed = ($bgfixed == 'fixed')? true : false;
		 $sp_nav_transparent = ($sp_nav_transparent == 'true')? true : false;
		 $style = $this->site_E->style;
		 if(!empty($style))
		 {
			$style = unserialize($style);
		 }
		 $style['theme_id'] = $theme_id;
		 
		 
		 $style['themelogo'] = $themelogo;	
		 $style['background_color'] = $background_color;
		 $style['banner_color'] = $banner_color;
		 $style['link_color'] = $link_color;
		 $style['tab_color'] = $tab_color;
		 $style['tab_link_color'] = $tab_link_color;
		 if(!empty($picid))
		 {
			 $pic_E = Pics::getById($picid);
			 $background_pic = $pic_E->pic;
			 $style['background_pic'] = $background_pic;
		 }
		 else if(!empty($background_pic))
		 {
		 	 $style['background_pic'] = $background_pic;
		 }
		 else 
		 {
		 	$style['background_pic'] = '';
		 }
		 $style['bgfixed'] = $bgfixed;	
		 $style['background_repeat'] = $background_repeat;
		 $style['background_pos'] = $background_pos;
		 $style['sp_nav_transparent'] = $sp_nav_transparent;	
		 $style['background_cancel'] = $background_cancel;
		 
		 
		 
		 $this->site_E->style = serialize($style);
		 $data['r'] = 0;
		 $UnitOfWork = E_UnitOfWork::getWork();
		 $UnitOfWork->commit();	
		 echo json_encode($data);
		 exit;
	}
	public function doBackground()
	{
		$pic_return = T_Picupload::save_pic('picfile', '/site/background');
		if($pic_return['error'] < 0)
		{
				$data['r'] = "1";
				$data['error'] = "图片大小问题";
				echo json_encode($data);
				exit;
		}
		$image_url = "/site/background/".$pic_return['newname'];
		
		$row['pic'] = $image_url;
		$row['userid'] = $this->user_E->id;
		$row['sign'] = 2;
		$pic_E = Pics::insertEntity($row);
		$UnitOfWork = E_UnitOfWork::getWork();
		$UnitOfWork->commit();	
		
		$data['ver'] = $pic_E->id;
		$data['r'] = 0;
		$data['pic'] = $image_url."?".rand();
		echo json_encode($data);
		exit;
	}
	public function doRoom()
	{
		
	}
	public function doCheckRoom()
	{
		$siteid = $this->site_E->id;
		$room_num = Rooms::getNum($siteid);
		if($room_num >= 6)
		{
			$data['code'] = 'reach_max_limit';
			$data['error'] = '最多能创建6个房间';
		}
		else
		{
			$data['code'] = 'go_ahead';
		}
		echo json_encode($data);
		exit;
	}
	public function doCreateRoom()
	{
		$siteid = $this->site_E->id;
		$room_num = Rooms::getNum($siteid);
		if($room_num < 6)
		{
			
			$row['siteid'] = $siteid;
			$row['userid'] = $this->user_E->id;
			$row['name'] = '未命名房间';
			$room_E = Rooms::insertEntity($row);
			$data['error'] = 0;
			$data['room'] = '/site/'.$siteid.'/'.$room_E->id;
		}
		 $UnitOfWork = E_UnitOfWork::getWork();
		 $UnitOfWork->commit();	
		echo json_encode($data);
		exit;
	}
}

?>
